let connection = require("../dbutil/dbutil");

function allphone(callback) {
    let sql = "SELECT * FROM mobile_view"
    connection.query(sql, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1001,
                data: result
            }
            callback(ret)
        }
    })
}

function allcomputer(callback) {
    let sql = "SELECT * FROM computer_view"
    connection.query(sql, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1001,
                data: result
            }
            callback(ret)
        }
    })
}

function getProductData(productName, callback) {
    let sql = "SELECT * FROM product WHERE pname = '" + productName + "' ORDER BY pprice ASC"
    connection.query(sql, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let data = []
            data = result
            callback(data)
        }
    })
}

function getEvaluate(productName, callback) {
    let sql = "SELECT * FROM `evaluate_view` WHERE pname = '" + productName + "'"
    connection.query(sql, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let data = []
            data = result
            callback(data)
        }
    })
}

exports.allphone = allphone;
exports.allcomputer = allcomputer;
exports.getProductData = getProductData;
exports.getEvaluate = getEvaluate;